//
//  OosSiteEntry.m
//  com.oos.kraken
//
//  Created by Pablo Gonzalez on 2/14/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import "OosServiceEntry.h"
#import "../../Utils/OosEnum+Internal.h"

@implementation OosOfferElement

+ (NSString *)extensionElementLocalName     { return @"offer"; }

- (void)addExtensionDeclarations 
{
    [super addExtensionDeclarations];
    [self addExtensionDeclarationForParentClass: [self class] 
                                   childClasses: [OosTitle class], [OosId class], 
     [OosSummary class], [OosOfferType class], [OosSignups class], [OosValue class], nil];
}

#pragma mark <oos:title>

- (OosTitle *)title
{
    return [self objectForExtensionClass:[OosTitle class]];
}

#pragma mark <oos:id>

- (OosId *)oosId
{
    return [self objectForExtensionClass:[OosId class]];
}

#pragma mark <oos:summary>

- (OosSummary *)summary
{
    return [self objectForExtensionClass:[OosSummary class]];
}

#pragma mark <oos:offerType>

- (OosOfferType *)offerType
{
    return [self objectForExtensionClass:[OosOfferType class]];
}

#pragma mark <oos:signups>

- (OosSignups *)signups
{
    return [self objectForExtensionClass:[OosSignups class]];
}

#pragma mark <oos:value>

- (OosValue *)value
{
    return [self objectForExtensionClass:[OosValue class]];
}

@end

@implementation OosServiceEntry

+ (OosServiceEntry *) siteEntry 
{ 
	OosServiceEntry *entry;
	entry = [[[self alloc] init] autorelease];
	[entry setNamespaces: [OosNamespaces defaultNamespace]];
	return entry; 
}

//+ (void) load 
//{
//	[self registerEntryClass];
//}

- (void) addExtensionDeclarations 
{
	[super addExtensionDeclarations];
	
	Class entryClass = [self class];
	
	[self addExtensionDeclarationForParentClass:entryClass childClasses: 
	 // Oos extensions
	 [OosUserAddress class], [OosUrl class], [OosTelephone class],
	 [OosId class], [OosSlug class], [OosReviewCounter class],
	 [OosSavedCounter class], [OosRating class], [OosLeadService class], 
     [OosPrivacyElement class], [OosAuthor class], [OosLists class], 
     [OosTags class], [OosSavedBy class], [OosOfferElement class], nil];
}

+ (NSString *)defaultServiceVersion 
{
	return @"2.0";
}

#pragma mark <oos:thelephone>

- (OosTelephone *) telephone						
{ 
	return [self objectForExtensionClass:[OosTelephone class]]; 
}
- (void) setTelephone: (OosTelephone *) telephone	
{ 
	[self setObject:telephone forExtensionClass:[OosTelephone class]];
}

#pragma mark <oos:id>

- (OosId *) oosId										
{ 
	return [self objectForExtensionClass:[OosId class]]; 
}
- (void) setOosId: (OosId *) oosId						
{ 
	[self setObject:oosId forExtensionClass:[OosId class]];
}

#pragma mark <oos:review-counter>

- (OosReviewCounter *) reviewsCounter				
{ 
	return [self objectForExtensionClass:[OosReviewCounter class]]; 
}
- (void) setReviewsCounter: (OosReviewCounter *) reviewsCounter				
{ 
	[self setObject:reviewsCounter forExtensionClass:[OosReviewCounter class]]; 
}

#pragma mark <oos:saved-counter>

- (OosSavedCounter *) savedCounter					
{ 
	return [self objectForExtensionClass:[OosSavedCounter class]]; 
}
- (void) setSavedCounter: (OosSavedCounter *) savedCounter			
{ 
	[self setObject:savedCounter forExtensionClass:[OosSavedCounter class]]; 
}

#pragma mark <oos:url>

- (OosUrl *) url									
{ 
	return [self objectForExtensionClass:[OosUrl class]]; 
}
- (void) setUrl: (OosUrl *) url						
{ 
	[self setObject:url forExtensionClass:[OosUrl class]];
}

#pragma mark <oos:lead-service>

- (OosLeadService *) leadService
{
	return [self objectForExtensionClass:[OosLeadService class]]; 
}

- (void) setLeadService:(OosLeadService *) leadService;
{
	[self setObject:leadService forExtensionClass:[OosLeadService class]];

}

#pragma mark <oos:rating>

- (void) setRating:(OosRating *) rating
{
	[self setObject:rating forExtensionClass:[OosRating class]];
}

- (OosRating *) rating
{
	return [self objectForExtensionClass:[OosRating class]]; 
}

#define PRIVACY_SCHEME @"http://api.11870.com/api/v2/privacy"

- (OosPrivacy *)privacy
{
    OosPrivacy *privacy = nil;
    OosPrivacyElement *privacyElement = [self objectForExtensionClass:[OosPrivacyElement class]];
    if (privacyElement) {
        privacy = [OosPrivacy enumForPosition:[privacyElement intValue]];
    } else {
        NSArray *categories = [GDataCategory categoriesWithScheme:PRIVACY_SCHEME 
                                                   fromCategories:[self categories]];
        if (categories.count > 0) {
            GDataCategory *category = [categories objectAtIndex:0];
            NSString *term = [category.term lowercaseString];
            NSUInteger index = [[OosPrivacy parseValues] indexOfObject:term];
            if (index != NSNotFound) {
                privacy = [OosPrivacy enumForPosition:index];
            }
        }
    }
    
    return privacy;
}

- (void)setPrivacy:(OosPrivacy *)privacy
{
    OosPrivacyElement *privacyElement = [OosPrivacyElement valueWithInt:privacy.ordinal];
    [self setObject:privacyElement forExtensionClass:[OosPrivacyElement class]];
    GDataCategory *privacyCategory = [GDataCategory categoryWithScheme: PRIVACY_SCHEME 
                                                                  term: privacy.description];
    [self addObject:privacyCategory forExtensionClass:[GDataCategory class]];
}

#pragma mark <oos:author>

- (OosAuthor *)author
{
    return [self objectForExtensionClass:[OosAuthor class]]; 
}

- (void)setAuthor:(OosAuthor *)author
{
    [self setObject:author forExtensionClass:[OosAuthor class]];
}

#pragma mark <oos:lists>

- (OosLists *)lists
{
    return [self objectForExtensionClass:[OosLists class]];
}

- (void)setLists:(OosLists *)lists
{
    [self setObject:lists forExtensionClass:[OosLists class]];
}


#pragma mark <oos:tags>

- (OosTags *)tags
{
    return [self objectForExtensionClass:[OosTags class]];
}

- (void)setTags:(OosTags *)tags
{
    [self setObject:tags forExtensionClass:[OosTags class]];
}

#pragma mark <oos:tag>

- (void)addCategoryTag:(OosCategoryTag *)tag
{
    [self addObject:tag forExtensionClass:[OosCategoryTag class]];
}

- (void)removeCategoryTag:(OosCategoryTag *)tag
{
    [self removeObject:tag forExtensionClass:[OosCategoryTag class]];
}

#pragma mark <oos:list>

- (void)addCategoryList:(OosCategoryList *)list
{
    [self addObject:list forExtensionClass:[OosCategoryList class]];
}

- (void) removeCategoryList:(OosCategoryList *)list
{
    [self removeObject:list forExtensionClass:[OosCategoryList class]];
}

#pragma mark <oos:saved-by>

- (OosSavedBy *)savedBy
{
    return [self objectForExtensionClass:[OosSavedBy class]];
}

- (void)setSavedBy:(OosSavedBy *)savedBy
{
    [self setObject:savedBy forExtensionClass:[OosSavedBy class]];
}

#pragma mark <oos:offer>

- (OosOfferElement *)offer
{
    return [self objectForExtensionClass:[OosOfferElement class]];
}

@end
